const { Quest } = require('../models')


async function findAll({attributes=null, where=null, offset = 0, limit = null, include=null,order=null }) {

    let result = await Quest.findAndCountAll({
        attributes,
        where,
        offset,
        limit,
        include,
        order,
        distinct: true
    })

    return result
}


function keySort(key,sortType) {
    return function(a,b){
        if (typeof a[key]==='number' && typeof b[key]==='number')
        {
            return sortType ? (b[key]-a[key]) : (a[key]-b[key]);
        }
        else if (typeof a[key]==='string' && typeof b[key]==='string')
        {
            let x = a[key].toLowerCase();
            let y = b[key].toLowerCase();
            if (x < y) {return sortType ?1:-1;}
            if (x > y) {return sortType ?-1:1;}
            return 0;
        }
        else
        {
            return 0;
        }
    }
}

module.exports ={
    join:findAll,
    keySort
}

